package com.Jano.servlet;

import com.alibaba.fastjson.JSON;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Calendar;

import static com.Jano.constant.BusinessConstant.*;

/**
 * description: OrderNumber <br>
 * date: 2022/6/10 3:19 PM <br>
 * author: zoumaoji <br>
 * version: 1.0 <br>
 */
@WebServlet(urlPatterns = "/apis/orderNumber")
public class OrderNumberServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try (JedisPool jedisPool = new JedisPool(REDIS_HOST, REDIS_POST);
             Jedis jedis = jedisPool.getResource()) {
            Calendar calendar = Calendar.getInstance();
            DecimalFormat decimalFormatDay = new DecimalFormat("000");
            DecimalFormat decimalFormatHour = new DecimalFormat("00");
            DecimalFormat decimalFormatIncrement = new DecimalFormat("00000000");
            String year = String.valueOf(calendar.get(Calendar.YEAR));
            String day = decimalFormatDay.format(calendar.get(Calendar.DAY_OF_YEAR));
            String hour = decimalFormatHour.format(calendar.get(Calendar.HOUR_OF_DAY));
            String increment = decimalFormatIncrement.format(jedis.incr("increment"));
            String orderNumber = year + day + hour + increment;
            resp.setContentType(MEDIA_TYPE_JSON);
            PrintWriter pw = resp.getWriter();
            pw.print(JSON.toJSONString(orderNumber, true));
        }
    }
}
